﻿using System;
using System.Collections.Generic;

namespace CSharpSnake
{
    class Program
    {
        static void Main()
        {
            //创建屏幕,贪吃蛇,食物实例
            int width = 20;
            int height = 10;
            Snake theSnake = new Snake(width, height, 'O', 4, 4, 8);
            Screen theScreen = new Screen(width, height, '-');
            Food theFood = new Food(width, height, '8');

            int fps = 5;
            DateTime lastFrameTime = System.DateTime.Now;
            Input input = new Input();
            bool gameOver = false;

            //定时执行循环，检测输入，并对蛇，屏幕进行操作
            while (!gameOver)
            {
                //异步输入检测
                List<ConsoleKey> recentInputs = input.ConsumeInput();
                if(recentInputs.Count > 0)
                {
                    ConsoleKey lastInput = recentInputs[recentInputs.Count - 1];
                    ESnakeDirection direction = ESnakeDirection.None;
                    if (lastInput == ConsoleKey.W || lastInput == ConsoleKey.UpArrow)
                    {
                        direction = ESnakeDirection.Up;
                    }else if(lastInput == ConsoleKey.S || lastInput == ConsoleKey.DownArrow)
                    {
                        direction = ESnakeDirection.Down;
                    }else if(lastInput == ConsoleKey.A || lastInput == ConsoleKey.LeftArrow)
                    {
                        direction = ESnakeDirection.Left;
                    }else if(lastInput == ConsoleKey.D || lastInput == ConsoleKey.RightArrow)
                    {
                        direction = ESnakeDirection.Right;
                    }
                    if(direction != ESnakeDirection.None && direction != theSnake.snakeBody[0].afterDirection)
                    {
                        theSnake.SetDirection(direction);
                    }
                }

                //蛇前进
                theSnake.Move();
                //更新像素数组，同时碰撞检测
                switch (theScreen.RenderSnake(theSnake.snakeBody))
                {
                    case CollideResult.SnakeBody:
                        gameOver = true;
                        break;
                    case CollideResult.Food:
                        theFood.exist = false;
                        theSnake.Grow(false);
                        break;
                    case CollideResult.NoCollid:
                        break;
                    default:
                        break;
                }
                if (!theFood.exist)
                {
                    theFood.SetPosition(theScreen.pixels);
                }
                theScreen.RenderFood(theFood);

                theScreen.Display();

                //游戏失败时打印失败提示
                if (gameOver)
                {
                    Console.WriteLine("game over");
                }

                //计算下次刷新时间
                DateTime currentFrameTime = System.DateTime.Now;
                double frameTime = (currentFrameTime - lastFrameTime).TotalMilliseconds;
                double remaindTime = 1000 / fps - frameTime;
                if (remaindTime > 0)
                {
                    System.Threading.Thread.Sleep((int)remaindTime);
                    lastFrameTime = System.DateTime.Now;
                }
                else
                {
                    lastFrameTime = currentFrameTime;
                }
            }
        }
    }
}
